
package edu.towson.cosc.cosc455.astewart.compiler;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
/**
 * Contains the main
 * Initialize arrayLists, lexer, parser and generator 
 * @author AndrewStewart
 *
 */
public class MyCompiler {
	
	public static String token; 
	public static String completeFile; 
	public static String text;
	static MyLexicalAnalyzer Lexer;
	static MySyntaxAnalyzer parser;
	static MySemanticAnalyzer generator;
	public static ArrayList<String> legalTokens;
	public static ArrayList<String> parseTree;
	public static ArrayList<String> variableTokens;
	public static String htmlFile; 
	
	public static void main(String[] args) {
		//add all legal tokens to the legal token parse tree
		token="";
		completeFile="";
		text="";
		variableTokens=new ArrayList<String>();
		legalTokens=new ArrayList<String>();
		legalTokens.add(LexicalTokens.DOC_BEGIN);
		legalTokens.add(LexicalTokens.DOC_END);
		legalTokens.add(LexicalTokens.HEAD_BEGIN);
		legalTokens.add(LexicalTokens.HEAD_END);
		legalTokens.add(LexicalTokens.COMMENT_BEGIN);
		legalTokens.add(LexicalTokens.COMMENT_END);
		legalTokens.add(LexicalTokens.PARAGRAPH_BEGIN);
		legalTokens.add(LexicalTokens.PARAGRAPH_END);
		legalTokens.add(LexicalTokens.VARIABLE_DEFINE_BEGIN);
		legalTokens.add(LexicalTokens.VARIABLE_DEFINE_CALL);
		legalTokens.add(LexicalTokens.VARIABLE_DEFINE_END);
		legalTokens.add(LexicalTokens.VARIABLE_USE_BEGIN);
		legalTokens.add(LexicalTokens.VARIABLE_USE_END);
		legalTokens.add(LexicalTokens.BOLD_BEGIN);
		legalTokens.add(LexicalTokens.BOLD_END);
		legalTokens.add(LexicalTokens.TITLE_BEGIN);
		legalTokens.add(LexicalTokens.TITLE_END);
		legalTokens.add(LexicalTokens.ITALICS_BEGIN);
		legalTokens.add(LexicalTokens.ITALICS_END);
		legalTokens.add(LexicalTokens.LIST_BEGIN);
		legalTokens.add(LexicalTokens.LIST_END);
		legalTokens.add(LexicalTokens.LIST_ITEM_BEGIN);
		legalTokens.add(LexicalTokens.LIST_ITEM_END);
		legalTokens.add(LexicalTokens.AUDIO_BEGIN);
		legalTokens.add(LexicalTokens.AUDIO_END);
		legalTokens.add(LexicalTokens.VIDEO_BEGIN);
		legalTokens.add(LexicalTokens.VIDEO_END);
		legalTokens.add(LexicalTokens.NEWLINE);
		
		
		//Check that there is 1 arg
		if (args.length !=1){
			System.err.println("ERROR! 1 argument required");
			System.exit(0);
		}
		File file= new File (args[0]);
		try{
			Scanner scanner=new Scanner(file);
			while (scanner.hasNextLine()){
				completeFile+= scanner.nextLine() + " ";
			}
		}catch (FileNotFoundException e){
			e.printStackTrace();
		}
		
		//check extension 
		String extension= "";
		String filename=file.getName();
		int i=filename.lastIndexOf('.');
		htmlFile=filename.substring(0,i);
		htmlFile+=".html";
		if(i>0){
			extension=filename.substring(i+1);
		}
		if (!extension.equals("lol")){
			System.err.println("ERROR, file must be a .lol file");
			System.exit(0);
		}
		
		//initialize parse tree array
		parseTree=new ArrayList<String>();
		
		
		Lexer=new MyLexicalAnalyzer();
		Lexer.getNextToken();
		
		parser= new MySyntaxAnalyzer();
		parser.lolcode();
		
		generator = new MySemanticAnalyzer(); 
		generator.writeOutput();
	}

}
